package com.zhiqianbao.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.zhiqianbao.R;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.entity.User;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;
import com.zhiqianbao.utils.ValidateUtils;

import java.util.HashMap;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2015/12/29.
 */
public class ChangePasswordActivity extends BaseActivity {
    /**
     * 显示密码1
     */
    ImageButton imageButtonDisplayPwd1;
    boolean flag1 = false;
    /**
     * 原密码输入框
     */
    EditText edittextOldPassword;
    /**
     * 显示密码2
     */
    ImageButton imageButtonDisplayPwd2;
    boolean flag2 = false;
    /**
     * 新密码输入框
     */
    EditText edittextNewPassword;
    /**
     * 显示密码3
     */
    ImageButton imageButtonDisplayPwd3;
    boolean flag3 = false;
    /**
     * 新密码确认框
     */
    EditText edittextNewPasswordTwo;
    /**
     * 确认按钮
     */
    Button passwordChangeOK;
    /**
     *
     */
    private Gson gson = new Gson();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        init();
        initView();
    }

    private void initView() {
         imageButtonDisplayPwd1=(ImageButton)findViewById(R.id.imageButton_display_pwd_1);
         edittextOldPassword=(EditText)findViewById(R.id.edittext_old_password);
         imageButtonDisplayPwd2=(ImageButton)findViewById(R.id.imageButton_display_pwd_2);
         edittextNewPassword=(EditText)findViewById(R.id.edittext_new_password);
         imageButtonDisplayPwd3=(ImageButton)findViewById(R.id.imageButton_display_pwd_3);
         edittextNewPasswordTwo=(EditText)findViewById(R.id.edittext_new_password_two);
         passwordChangeOK=(Button)findViewById(R.id.password_change_OK);
        ButtonListener listener = new ButtonListener();

        imageButtonDisplayPwd1.setOnClickListener(listener);
        imageButtonDisplayPwd2.setOnClickListener(listener);
        imageButtonDisplayPwd3.setOnClickListener(listener);
        passwordChangeOK.setOnClickListener(listener);

    }

    private class ButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if(R.id.imageButton_display_pwd_1==v.getId()) {
                if (flag1) {
                    imageButtonDisplayPwd1.setImageResource(R.drawable.ac_eye);
                    edittextOldPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    flag1 = false;
                } else {
                    imageButtonDisplayPwd1.setImageResource(R.drawable.ac_open_eye);
                    edittextOldPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    flag1 = true;
                }
            }
            else if(R.id.imageButton_display_pwd_2==v.getId()) {
                if (flag2) {
                    imageButtonDisplayPwd2.setImageResource(R.drawable.ac_eye);
                    edittextNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    flag2 = false;
                } else {
                    imageButtonDisplayPwd2.setImageResource(R.drawable.ac_open_eye);
                    edittextNewPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    flag2 = true;
                }
            }
            else if(R.id.imageButton_display_pwd_3==v.getId()) {
                if (flag3) {
                    imageButtonDisplayPwd3.setImageResource(R.drawable.ac_eye);
                    edittextNewPasswordTwo.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    flag3 = false;
                } else {
                    imageButtonDisplayPwd3.setImageResource(R.drawable.ac_open_eye);
                    edittextNewPasswordTwo.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    flag3 = true;
                }
            }
            else if(R.id.password_change_OK==v.getId()){
                    String oldPassword = edittextOldPassword.getText().toString();
                    String newPassword = edittextNewPassword.getText().toString();
                    String newPasswordTwo = edittextNewPasswordTwo.getText().toString();
                    if (StringUtils.isBlank(oldPassword)) {
                        showShortToast1("请输入原密码");
                        return;
                    }
                    if (StringUtils.isBlank(newPassword)) {
                        showShortToast1("请输入新密码");
                        return;
                    }
                    if (StringUtils.isBlank(newPasswordTwo)) {
                        showShortToast1("请再次输入新密码");
                        return;
                    }
                    if (!ValidateUtils.isPassword(newPassword)) {
                        showShortToast1("密码为6-12位数字或字母");
                        return;
                    }
                    if (!newPasswordTwo.equals(newPassword)) {
                        showShortToast1("两次密码输入不一致");
                        return;
                    }
                    //TODO 调用修改密码接口
                    Map<String, String> param = new HashMap<>();
                    param.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                    param.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                    param.put("oldPassword", oldPassword);
                    param.put("newPassword", newPassword);
                    new ChangePasswordAsyncTask().execute(param);
            }
        }
    }

    private class ChangePasswordAsyncTask extends AsyncTask<Map, Void, Result<User>> {
        @Override
        protected void onPreExecute() {
            showWaitDialog();
        }
        @Override
        protected Result<User> doInBackground(Map... params) {
            String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/changePassword", params[0], ChangePasswordActivity.this);
            if (StringUtils.isBlank(result)) {
                return null;
            }
            Result resultObject = gson.fromJson(result, Result.class);
            return resultObject;
        }
        @Override
        protected void onPostExecute(Result result) {
            if (result == null) {
                showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                return;
            }
            if(Constants.DEFAULT_SUCCESS_CODE.equals(result.getCode())) {
//                showShortToast1(result.getMsg());
                User loginUser = gson.fromJson(gson.toJson(result.getData()), User.class);
                String token1 = loginUser.getToken();
                String token = Constants.LOGIN_USER.getToken();

                loginUser.setToken(token);
                Constants.LOGIN_USER = loginUser;
                //更新数据库
                User.deleteAll(User.class);
                loginUser.save();
              //  showShortToast1(result.getMsg());
                ChangePasswordActivity.this.finish();
            } else {
                showShortToast1(result.getMsg());
            }
            closeWaitDialog();
        }

    }
}

